<template>
  <div class="app-container">
    设置添加
    <el-form label-width="120px">
      <el-form-item label="地区">
        <el-input v-model="virusInfo.point"/>
      </el-form-item>
      <el-form-item label="毒株类型">
      <el-tag
        :key="tag"
        v-for="tag in virusInfo.category"
        closable
        :disable-transitions="false"
        @close="handleClose(tag)">
        {{tag}}
      </el-tag>
      <el-input
        class="input-new-tag"
        v-if="inputVisible"
        v-model="inputValue"
        ref="saveTagInput"
        size="small"
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
      >
      </el-input>
      <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="virusInfo.note"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import virus from '@/api/virus'
export default{
  data(){
    return{
      virusInfo:{
        point:"",
        category:[],
        note:"",
      },
      inputValue:"",
      inputVisible:false
    };
  },
  created() {
    if(this.$route.params&&this.$route.params.id){
      const id=this.$route.params.id
      this.showInfo(id)
    }else{
      this.virusInfo={
        point:"",
          category:[],
          note:"",
      }
    }
  },
  methods:{
    showInfo(id){
      virus.showVirusByid(id)
      .then(response=>{
        this.virusInfo=response.data
      })
    },
    handleClose(tag){
      this.virusInfo.category.splice(this.virusInfo.category.indexOf(tag), 1);
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.virusInfo.category.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
      console.log(this.virusInfo.category)
    },
    saveOrUpdate(){
    virus.save(this.virusInfo)
      .then(response=>{
        console.log(this.list)
        this.$router.push({path:'/virus/list'})
      }).catch(error=>{//请求失败
        console.log(error)
      })
    }
  }
}
</script>

<style>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
